$start = Get-Date
$array = @()
$DC='xxxx.domain.internal'
Clear-Content .\MonthReport_user.csv
$AllUsers = get-aduser -Server $DC -Filter *
# $AllUsers = Get-Content .\UserID.txt
# $AllUsers = get-aduser -Server $DC -Filter * -Searchbase "OU=xxxx,OU=xxx,OU=xx,OU=x,DC=domain,DC=internal"
foreach($user in $AllUsers){
	$userinfo = Get-ADUser -Server $DC -Identity $user  -Properties *
	if($userinfo.Mail -eq $null){$mail = "No EA Email"}
		else{$mail =$userinfo.Mail}
	if($userinfo.Department -eq $null){$Department = "Not Provided"}
		else{$Department =$userinfo.Department}
	if($userinfo.Company -eq $null){$Company = "Not Provided"}
		else{$Company =$userinfo.Company}
	if($userinfo.Enabled -imatch "TRUE"){$Enabled = "Enabled"}
		else{$Enabled ="Disabled"}
	if($userinfo.AccountExpirationDate -eq $null){$AccountExpirationDate = "Never Expired"}
		else{$AccountExpirationDate ="+"+$userinfo.AccountExpirationDate.ToString("MM/dd/yyyy HH:mm:ss")}
	if($userinfo.LastLogonDate -ne $null){$LastLogonDate ="+"+$userinfo.LastLogonDate.ToString("MM/dd/yyyy HH:mm:ss")}else{$LastLogonDate=""}
	if($userinfo.PasswordLastSet -eq $null){
		$PasswordExpirationDate="Already Expired"
		$PasswordLastSet=""
		}
	else{
		$PasswordExpirationDate = "+"+$userinfo.PasswordLastSet.adddays(90).ToString("MM/dd/yyyy HH:mm:ss")
		$PasswordLastSet ="+"+$userinfo.PasswordLastSet.ToString("MM/dd/yyyy HH:mm:ss")
	}
	if($userinfo.PasswordNeverExpires -imatch "TRUE"){$PasswordExpirationDate = "Never Expired"}
#<---------------------------------------Modify based on user GECOS/Description
	if($userinfo.Description -eq $null){
		$IBMorEAorREVIEW= "REVIEW"
		$AccountType = "REVIEW"
	}
	else{
		$ibm='/I/', '/N/', '/T/', '/E/', '/V/', '/F/','/S/' 
		$reslut=1..7
		for ($n =0;$n -lt 7;$n++){
			$reslut[$n]=$userinfo.Description.toupper().contains($ibm[$n])
		}
		if($reslut -contains 'True'){
			$IBMorEAorREVIEW="IBM"
			if ($reslut[5] -contains 'True' -or $reslut[6] -contains 'True' ){
				$AccountType = "NON-PEOPLE"
			}
			else{
				$AccountType = "PEOPLE"
			}
		}
		elseif($userinfo.Description.toupper().contains('/C/')){
			$IBMorEAorREVIEW="EA"
			$AccountType = "REVIEW"}
		else{
			$IBMorEAorREVIEW= "REVIEW"
			$IBMorEAorREVIEW="Not Provided"
		}
	}
#<---------------------------------------Modify based on group info
	$grp = (Get-ADUser -Server $DC -identity $user -Properties memberof).memberof
	if($grp -imatch 'GBL_DenyLogonLocally'){$InteractiveLogonStatus = "DISABLED"}
		else{$InteractiveLogonStatus = "ENABLED"}
#<---------------------------------------Modify Password Expiration Date
	$Properties = @{`
		"DisplayName" = $userinfo.DisplayName;`
		"SamAccountName" = $userinfo.SamAccountName;`
		"Description" = $userinfo.Description;	`
		"Last Name" = $userinfo.Surname;	`
		"First Name" = $userinfo.GivenName;`
		"Manager" = $userinfo.Manager;`
		"DistinguishedName" = $userinfo.DistinguishedName;`
		"E-mail Address" = $Mail;`
		"Department" = $Department;`
		"Company" = $Company;`
		"When Created" = "+"+$userinfo.whenCreated.ToString("MM/dd/yyyy HH:mm:ss");`
		"Last Logon Time" = $LastLogonDate;`
		"User Logon Count"=$userinfo.logonCount;`
		"Account Status" = $Enabled;`
		"Account Expir" = $AccountExpirationDate;`
		"Pwd Never Ex" = $userinfo.PasswordNeverExpires;`
		"When Change" = $PasswordLastSet;`
		"Password Expir"=$PasswordExpirationDate;`
		"Interactive Logon Status" = $InteractiveLogonStatus;`
		"IBM or EA"=$IBMorEAorREVIEW;`
		"Account Type"=$AccountType
	}
	$Newobject = New-Object  PSObject -Property  $Properties
	$Array +=$Newobject
}

$Array | Select-Object `
		"DisplayName",`
		"SamAccountName",`
		"Description",`
		"Last Name",`
		"First Name",`
		"Manager",`
		"DistinguishedName",`
		"E-mail Address",`
		"Department",`
		"Company",`
		"When Created",`
		"Last Logon Time",`
		"User Logon Count",`
		"Account Status",`
		"Account Expir",`
		"Pwd Never Ex",`
		"When Change",`
		"Password Expir",`
		"Interactive Logon Status",`
		"IBM or EA",`
		"Account Type"  `
| Export-CSV .\MonthReport_user.csv -UseCulture -NoTypeInformation -Encoding UTF8
#get-content .\temp.csv  | Add-Content .\MonthReport_user.csv
$end = Get-Date
Write-Host -ForegroundColor yellow ('Total Runtime: ' + ($end - $start).TotalMinutes)
Send-MailMessage `
	-from $EAMail `
	-to $NotesMail `
	-subject "Monthly AD Report" `
	-Attachments .\MonthReport_user.csv `
	-smtpserver xxxx `
	-Encoding ([System.Text.Encoding]::UTF8)